package pl.edu.pw.stud.ltrzaska.mvc.view;

/*
 * DisplayViewPanel.java
 *
 * Created on January 22, 2007, 2:36 PM
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.util.List;
import java.util.concurrent.BlockingQueue;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

import org.apache.log4j.Logger;

import pl.edu.pw.stud.ltrzaska.mvc.model.Model;

public class LoggingPanel extends AbstractViewPanel {

	private static final Logger LOGGER = Logger.getLogger(LoggingPanel.class);
	private static final long serialVersionUID = 2911957653137909730L;
	private JTextPane jta;
	private JScrollPane jsp;
	StyleContext sc = new StyleContext();
	private DefaultStyledDocument doc = new DefaultStyledDocument(sc);

	static SimpleAttributeSet ITALIC_GRAY = new SimpleAttributeSet();
	static SimpleAttributeSet RED = new SimpleAttributeSet();
	static SimpleAttributeSet BLACK = new SimpleAttributeSet();
	static SimpleAttributeSet ORANGE = new SimpleAttributeSet();

	public LoggingPanel(BlockingQueue<ViewEvent> controllerQueue) {
		super(controllerQueue);
		initComponents();
	}

	private void initComponents() {
		super.setLayout(new BorderLayout());
		setSize(500, 500);

		jta = new JTextPane(doc);
		jta.setBackground(new Color(50, 50, 50));
		jta.setForeground(new Color(255, 255, 255));
		jta.setMargin(new Insets(5, 5, 5, 5));
		jta.setEditable(false);

		StyleConstants.setForeground(RED, Color.red);
		StyleConstants.setBold(RED, true);
		StyleConstants.setFontFamily(RED, "Helvetica");
		StyleConstants.setFontSize(RED, 12);

		StyleConstants.setForeground(BLACK, Color.white);
		StyleConstants.setFontFamily(BLACK, "Helvetica");
		StyleConstants.setFontSize(BLACK, 12);

		StyleConstants.setForeground(ORANGE, Color.orange);
		StyleConstants.setFontFamily(ORANGE, "Helvetica");
		StyleConstants.setFontSize(ORANGE, 12);
		
		jsp = new JScrollPane(jta);
		add(jsp);
	}

	protected void insertText(String text, AttributeSet set) {
		try {
			jta.getDocument().insertString(jta.getDocument().getLength(), text, set);
		} catch (BadLocationException e) {
			LOGGER.error("", e);
		}
	}

	public void modelPropertyChange(PropertyChangeEvent evt) {
		LOGGER.debug("Model property change" + evt.getPropertyName());

		Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
		final Style errorStyle = sc.addStyle("MainStyle", defaultStyle);
		StyleConstants.setForeground(errorStyle, new Color(255, 0, 0));

		if (evt.getPropertyName().equals(Model.USER_MESSAGES_PROPERTY)) {
			List<UserMessage> uml = (List<UserMessage>) evt.getNewValue();
			LOGGER.debug("Model property change" + evt.getPropertyName());
			UserMessage m = uml.get(uml.size() - 1);
			if (m.getLevel() == UserMessageLevel.ERROR) {
				insertText(new StringBuffer("- ").append(m.getText()).append("\n").toString(), RED);
			} else if (m.getLevel() == UserMessageLevel.VIOLATION) {
				insertText(new StringBuffer("- ").append(m.getText()).append("\n").toString(), ORANGE);
			} else {
				insertText(new StringBuffer("- ").append(m.getText()).append("\n").toString(), BLACK);
			}
		}
		refresh();
	}

	public void refresh() {
		revalidate();
		repaint();
	}
}
